using System.Windows.Browser;

namespace MediaOwl.Common
{
    public class HelperService
    {
        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        private static string StripTagsCharArray(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
                return string.Empty;

            var array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            foreach (char let in source)
            {
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        /// <summary>
        /// Decodes a html-string and strips the tags.
        /// </summary>
        /// <param name="source">The source-string.</param>
        /// <returns>A formatted string.</returns>
        public static string FormatHtmlString(string source)
        {
            return HttpUtility.HtmlDecode(StripTagsCharArray(source));
        }
    }
}